package com.gzxw.mogublog.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxw.mogublog.commons.entity.Admin;
import com.gzxw.mogublog.commons.entity.Blog;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.xo.service.AdminService;
import com.gzxw.mogublog.xo.vo.AdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 获取管理员列表
    @PostMapping("/getList")
    public R getList(@RequestBody AdminVO adminVO) {

        IPage<Admin> pageList = adminService.getList(adminVO);

        Map<String, Object> result = new HashMap<>();
        List<Admin> records = pageList.getRecords();
        long total = pageList.getTotal();
        long size = pageList.getSize();
        long current = pageList.getCurrent();
        result.put("records", records);
        result.put("total", total);
        result.put("size", size);
        result.put("current", current);
        return R.success("200", result);
    }

    // 重置用户密码
    @PostMapping("/restPwd")
    public R restPwd(@RequestBody AdminVO adminVO) {

        String message = adminService.restPwd(adminVO);

        return R.success("200", message);
    }

    @PostMapping("/add")
    public R add(@RequestBody AdminVO adminVO) {

        String message = adminService.addAdmin(adminVO);

        return R.success("200", message);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody AdminVO adminVO) {

        String message = adminService.editAdmin(adminVO);

        return R.success("200", message);
    }

    @PostMapping("/delete")
    public R delete(@RequestParam List<String> adminUids) {

        String message = adminService.deleteBatchAdmin(adminUids);

        return R.success("200", message);
    }


}
